package sushil.luc.ticket;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import sushil.luc.dtos.CustomerDTO;
import sushil.luc.dtos.ItemDTO;
import sushil.luc.dtos.OrderDTO;
import sushil.luc.dtos.TicketDTO;
import sushil.luc.item.Item;
import sushil.luc.item.ItemStatus;
import sushil.luc.msc.Customer;

public class Ticket {
	
	private int id;
	private String TicketID;
	private Customer TicketCustomer;
	private List<Item> Items;
	private TicketStatus Status;
	private String CreationDate;
	private String DeliveryDate;
	

    public Ticket(TicketDTO ticketDTO, OrderDTO orderDTO){
    	this.setId(ticketDTO.getId());
        this.Items = new ArrayList<Item>();
        this.TicketID = String.valueOf(ticketDTO.getIdentifier());

        CustomerDTO customerDto = orderDTO.getCustomer();
        Customer customer = new Customer(customerDto, orderDTO.getInfo());

        this.TicketCustomer = customer;
        
        this.CreationDate = orderDTO.getInfo().getCreationDate();
        this.DeliveryDate = orderDTO.getInfo().getDeliveryDate();
        
        // get all the items and create real item objects from the dto objects
        List<ItemDTO> itemDtos = ticketDTO.getItems();
        
        if (itemDtos!=null)
        {
	        for(ItemDTO itemDto:itemDtos){
	            Item tmp = new Item(itemDto);
	            Items.add(tmp);
	        }
        }
        
        // check the status of the ticked
        calcTicketStatus();
    }

	
	/**
	 * Checks if the given RFID is contained in this ticket 
	 * @param check
	 * @return true if the RFID is contained, false if not
	 */
	private boolean checkItem(String check)
	{
		boolean result = false;
		
		int i =0;
		// loop over all items of the ticket
		while (i<this.Items.size() && !result)
		{
			String rfid = this.Items.get(i).getRFID();
			if (rfid!=null && rfid.equals(check))
			{
				// found the item.
				result =true;		
			}
			i++;
		}
		
		return result;
	}
	

	/**
	 * Checks if the given RFID is contained in the current Itemlist of the Ticket
	 * @param RFID
	 */
	public boolean checkRFIDInTicket (String RFID)
	{
		boolean res= checkItem(RFID);
		if (res)
		{
			//Maybe status changed 
			calcTicketStatus ();
		}
		
		return res;
	}
	
	/**
	 * Check the current status of the items and change the Ticket the same way
	 */
	public void calcTicketStatus ()
	{
		boolean res =true;
	//	check all the items from the Ticket
		for(int i=0; i<this.Items.size();i++)
		{
			Item tmp = this.Items.get(i);
			if (tmp.getStatus().equals(ItemStatus.Staged))
			{
				res = res && true;
			}
			else
			{
				res = res && false;
			}
		}
		
		// if all the items are staged, change the TicketSatus to staged
		if (res)
			this.setStatus(TicketStatus.Staged);
		else
			this.setStatus(TicketStatus.Checked);
	}
	
	public String getTicketID() {
		return TicketID;
	}

	public void setTicketID(String ticketID) {
		TicketID = ticketID;
	}

	public Customer getTicketCustomer() {
		return TicketCustomer;
	}

	public void setTicketCustomer(Customer ticketCustomer) {
		TicketCustomer = ticketCustomer;
	}

	public List<Item> getItems() {
		return Items;
	}

	public void setItems(List<Item> items) {
		Items = items;
	}

	public TicketStatus getStatus() {
		return Status;
	}

	public void setStatus(TicketStatus status) {
		Status = status;
	}

	public String getCreationDate() {
		return CreationDate;
	}

	public void setCreationDate(String creationDate) {
		CreationDate = creationDate;
	}


	public String getDeliveryDate() {
		return DeliveryDate;
	}


	public void setDeliveryDate(String deliveryDate) {
		DeliveryDate = deliveryDate;
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}
	
}
